package com.siwen.service.impl;

import com.siwen.mapper.SystemMapper;
import com.siwen.mapper.UserMapper;
import com.siwen.pojo.ChildTbl;
import com.siwen.pojo.system.*;
import com.siwen.result.ChildTblResult;
import com.siwen.result.Pager;
import com.siwen.service.SystemService;
import com.siwen.util.SysResultUtil;
import com.siwen.util.VeDate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SystemServiceImpl implements SystemService {
    @Resource
    private SystemMapper systemMapper;
    @Resource
    private UserMapper userMapper;
    @Override
    public List<ModuleResult> TheMenuAll() {
        List<SysModule> list=systemMapper.TheMenuAll();
        List<ModuleResult> resultList=new ArrayList<>();
        for (SysModule sysModule : list) {
            if(sysModule.getTier()==1){
                ModuleResult moduleResult=new ModuleResult();
                moduleResult.setTitle(sysModule.getModulename());
                moduleResult.setHref(sysModule.getUrl());
                List<ModuleResult> resultList2=new ArrayList<>();
                for (SysModule sysModule1 : list) {
                    if(sysModule1.getParentid()==sysModule.getModuleid()){
                        ModuleResult moduleResult1=new ModuleResult();
                        moduleResult1.setTitle(sysModule1.getModulename());
                        moduleResult1.setHref(sysModule1.getUrl());
                        resultList2.add(moduleResult1);
                    }
                }
                moduleResult.setChildren(resultList2);
                resultList.add(moduleResult);

            }
        }
        return  resultList;
    }

    @Override
    public ChildTblResult ChildTblAll(long curPage,int pageSize,String authenname,String startTime,String endTime) {
        long totalRecords = systemMapper.ChildTblAllCount(authenname,startTime,endTime);
        ChildTblResult result = new ChildTblResult();
        if (totalRecords >0){
            Pager pager = new Pager(totalRecords, pageSize, curPage);
        List<ChildTbl> list = systemMapper.ChildTblAll(pager.getStartIndex(), pager.getEndIndex(), authenname,startTime,endTime);
            result.setData(list);
            }
        result.setCode(0);
        result.setMsg("查询用户成功");

        result.setCount(totalRecords);

        return result;
    }

    @Override
    public ChildTblResult updateChildTblById(ChildTbl c) {
        c.setModifydate(VeDate.getStringDate());

        int pd=systemMapper.updateChildTblById(c);
        ChildTblResult result = new ChildTblResult();
        if (pd>0){
            result.setCode(0);
            result.setDatacount(pd);
            result.setMsg("修改用户成功");

            systemMapper.insertSysAdminczrztbl(
                    SysResultUtil.updateSysAdminczrztbl(c.getModifyuser(),c.getChildid()));
        }else {
            result.setCode(7);
            result.setMsg("修改用户失败");
        }
        return result;
    }

    @Override
    public ChildTblResult deleteChildTblById(String childid, String modifyuser) {
        String[] split = childid.split(",");
        int pd=0;
        for (int i = 0; i < split.length; i++) {
            System.out.println(split[i]);
            pd+=systemMapper.deleteChildTblById(split[i]);
        }

        ChildTblResult result = new ChildTblResult();
        if (pd>0){
            result.setCode(0);
            result.setDatacount(pd);
            result.setMsg("删除用户成功");
            systemMapper.insertSysAdminczrztbl(
                    SysResultUtil.deleteSysAdminczrztbl(modifyuser,childid));


        }else {
            result.setCode(7);
            result.setMsg("删除用户失败");
        }
        return result;
    }

    @Override
    public ChildTblResult insertSysAdminczrztbl(SysAdminczrztbl sysAdminczrztbl) {
        int pd=systemMapper.insertSysAdminczrztbl(sysAdminczrztbl);

        ChildTblResult result = new ChildTblResult();
        if (pd>0){
            result.setCode(0);
            result.setDatacount(pd);
            result.setMsg("添加操作记录成功");
        }else {
            result.setCode(7);
            result.setMsg("添加操作记录失败");
        }
        return result;
    }

    @Override
    public ChildTblResult insertSysUserfwjltbl(SysUserfwjltbl sysUserfwjltbl) {
        int pd=systemMapper.insertSysUserfwjltbl(sysUserfwjltbl);

        ChildTblResult result = new ChildTblResult();
        if (pd>0){
            result.setCode(0);
            result.setDatacount(pd);
            result.setMsg("添加访问记录成功");
        }else {
            result.setCode(7);
            result.setMsg("添加访问记录失败");
        }
        return result;
    }

    @Override
    public ChildTblResult insertChildTbl(ChildTbl c) {
        c.setCreatedate(VeDate.getStringDate());
        int pd=systemMapper.insertChildTbl(c);

        ChildTblResult result = new ChildTblResult();
        if (pd>0){
            result.setCode(0);
            result.setDatacount(pd);
            result.setMsg("添加用户成功");
            systemMapper.insertSysAdminczrztbl(SysResultUtil.insertSysAdminczrztbl(c.getCreateuser(),c.getAuthenname()));

        }else {
            result.setCode(7);
            result.setMsg("添加用户失败");
        }
        return result;
    }

    @Override
    public List<SysAdminRole> selectSysAdminRoleByid(String adminid) {
        return systemMapper.selectSysAdminRoleByid(adminid);
    }

    @Override
    public ChildTblResult sysLogin(String admincode, String password,String ip) {
        ChildTblResult<SysUserInformationTbl> result=new ChildTblResult<>();
        if("".equals(admincode) || "".equals(password) || admincode==null || null==password){
            result.setMsg("账号密码错误");
            result.setCode(7);
            return  result;
        }
        SysUserInformationTbl sysuser=userMapper.login(admincode,password);

       if(sysuser==null){
           result.setMsg("账号密码错误");
           result.setCode(7);
           return  result;
       }

        result.setMsg("登录成功");
        result.setCode(0);
       result.setOnedata(sysuser);
      SysAdminfwrztbl sysAdminfwrztbl=new SysAdminfwrztbl();

      sysAdminfwrztbl.setAdminid(String.valueOf(sysuser.getAdminid()));
      sysAdminfwrztbl.setAdminname(sysuser.getAdminname());
      sysAdminfwrztbl.setFwip(ip);
      sysAdminfwrztbl.setFwtime(new Date());

       systemMapper.insertSysAdminfwrztbl(sysAdminfwrztbl);

        return  result;
    }

}
